//检测新版本
const isNewVersion = function () {
  const updateManager = wx.getUpdateManager()
  updateManager.onUpdateReady(function () {
    wx.showModal({
      title: '更新提示',
      showCancel: false,
      confirmColor:'#e61211',
      content: '新版本已经准备好，立即重启应用',
      success(res) {
        if (res.confirm) {
          // 新的版本已经下载好，调用 applyUpdate 应用新版本并重启
          updateManager.applyUpdate()
        }
      }
    })
  })
}

//  监听网络状态
const watchNetworkState = function () {
  wx.onNetworkStatusChange((res) => {
    this.globalData.isNetwork = res.isConnected;
  })
}

//  设置设备信息
const setSystemInfo = function () {
  const res = wx.getSystemInfoSync()
  this.globalData.systemInfo = res
}

//  设置自定义顶部导航信息
const setCustomNav = function () {
  let menuButtonObject = wx.getMenuButtonBoundingClientRect(),
    systemInfo = this.globalData.systemInfo;
  //导航高度
  let statusBarHeight = systemInfo.statusBarHeight,
    navTop = menuButtonObject.top,
    navHeight = statusBarHeight + menuButtonObject.height + (menuButtonObject.top - statusBarHeight) * 2;
  this.globalData.customNavInfo = {
    navHeight,
    navTop,
    buttonHeight: menuButtonObject.height,
    buttonWidth: menuButtonObject.width,
    buttonRight: systemInfo.windowWidth - menuButtonObject.right,
    windowHeight: systemInfo.windowHeight
  }
}

module.exports = {
  data: {},
  methods: {
    // 检测新版本
    isNewVersion,
    // 监听网络状态
    watchNetworkState,
    // 设置设备信息
    setSystemInfo,
    // 设置自定义顶部导航信息
    setCustomNav
  }
}